const ModelMusicAlbum = require('../../../models/music/musicAlbum'),
    FileTool = require('../../../utils/filetool'),
    ComTool = require('../../../utils/commonTools');

const fileSavePath = "/images/album_cover_src";
// 删除专辑
exports.delMusicAlbum = (req, res, next) => {
    const { album_id, album_pic, album_pic_name } = req.query;
    ModelMusicAlbum.delMusicAlbum({ album_id }
        , (value) => {
            if (album_pic && album_pic_name)
                FileTool.removeFile('./public' + album_pic, album_pic_name);
            res.render('success', {
                data: JSON.stringify({
                    message: "删除成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "删除失败",
                    status: 200,
                    error
                })
            })
        });
}
// 编辑专辑
exports.updateMusicAlbum = async (req, res, next) => {
    const { album, file } = req.body;
    let newData = file ? await FileTool.saveImage(file, './public' + fileSavePath) : null;

    if (newData === false) {
        return res.render('fail', {
            data: JSON.stringify({
                message: "图片保存失败",
                status: 200,
            })
        })
    } else {
        if (album.album_pic && album.album_pic_name)
            FileTool.removeFile('./public' + album.album_pic, album.album_pic_name);
    }

    let Data = {
        ...album,
        album_pic: newData ? fileSavePath + '/' + newData.saveFloder + '/' : null,
        album_pic_name: newData ? newData.name : null,
    }

    const { album_id } = Data;
    if (album_id) {
        delete Data.album_id;
        ModelMusicAlbum.updateMusicAlbum(Data, {
            album_id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}

/** 添加专辑 */
exports.addMusicAlbum = (req, res, next) => {
    ModelMusicAlbum.addMusicAlbum({
        ...req.body,
        album_id: ComTool.orderCode()
    },
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}
/** 查找专辑 */
exports.findMusicAlbum = async (req, res, next) => {
    let MusicAlbum = await ModelMusicAlbum.findMusicAlbum(req.body);
    MusicAlbum.success((value) => {
        if (value.length) {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到专辑",
                    value: value,
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到专辑",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    MusicAlbum.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}
//查找所有专辑
exports.list = (req, res, next) => {
    const { isAll } = req.body;
    ModelMusicAlbum.findAll(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        }, isAll);
}